<?php
namespace App\Http\Controllers\Partner;
use App\Http\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Models\PartnerModels\Partner;
use App\Models\OrderModels\Order;
use App\Models\PartnerModels\Mobile;
class MobileAjaxController extends ApiBaseController{
	public function __construct(Request $request) {
		parent::__construct($request);
	}
    //移动端ajax控制器
	public function dataList(){
		$oMobile = new Mobile();
		$result = $oMobile->getMobilepPageData($this->params);
		echo json_encode($result);
	}

    function viewInfo(){
        $oMobile = new Mobile();
        $re=$oMobile->viewVipInfo($this->params['kid']);
        if($re){
            $this->returnJson(0,$re);
        }
        $this->returnJson(1,"数据错误");
    }
    /**
     * 操作状态
     * 开启/关闭
     */
    public function deleteData(){
        $this->params['update_time']=date('Y-m-d H:i:s',time());
        if(isset($this->params['id']) && intval($this->params['id']) > 0 && isset($this->params['status'])){
            $oMobile = new Mobile();
            if($oMobile->db_update('',$this->params['id'],['status'=>$this->params['status'],'update_time'=> $this->params['update_time']])){
                $this->data = true;
            }else{
                $this->code = 1;
                $this->msg = '操作失败';
            }
        }else{
            $this->code = 1;
            $this->msg = '缺少数据';
        }
        $this->boot();
    }

    //添加会员操作
    public function createSave(){
        $params = $this->params;
        $oMobile = new Mobile();
       // echo "<pre>";print_r($params);exit();
        $this->checkParams(0,$params);
        $pid=
        //生成移动端账号信息
        $data=[
            'user_name'=>trim($params['user_name']),
            'pwd'=>\App\Components\Tools::passwordEncryption(trim($params['pwd'])),
            'type'=>$params['type'],
            'status'=>1,
            'mobile'=>$params['mobile'],
            'nickname'=>$params['nickname'],
            'gender'=>intval($params['gender'])>0 ? intval($params['gender']) : 1,
            'create_time'=>date('Y-m-d H:i:s',time()),
            'update_time'=>date('Y-m-d H:i:s',time()),
            'start_date'=>mb_substr($this->params['times'],0,19),
            'end_date'=>mb_substr($this->params['times'],22),
            'remark'=>isset($params['remark']) ? $params['remark'] : '',
        ];
        $kid=$oMobile->db_insertGetId($data);
        if($kid){

            $this->returnJson(0,'添加成功',URL('admin/mobile/index'));
        }
        $this->returnJson(1,'添加失败');
    }

    //修改会员操作
    public function editSave(){
        $params = $this->params;
       // print_r($params);
        $this->checkParams(1,$params);
        $oMobile = new Mobile();
        //查询该会员是否存在
        $partnerData = $oMobile->selectVip($params['kid']);
  //  echo "<pre>";print_r($partnerData);exit();
        !$partnerData ? $this->returnJson(1,'数据错误') : '';

        //密码更改
        $pwd=$partnerData['pwd'];//原密码
        if( trim($params['pwd']) ){
            $pwd=\App\Components\Tools::passwordEncryption(trim($params['pwd']));
        }
        //生成会员信息 htmlspecialchars作用转换特色符号
        $data=[
            'user_name'=>trim($params['user_name']),
            'pwd'=>\App\Components\Tools::passwordEncryption(trim($params['pwd'])),
            'type'=>$params['type'],
            'status'=>1,
            'mobile'=>$params['mobile'],
            'nickname'=>$params['nickname'],
            'gender'=>intval($params['gender'])>0 ? intval($params['gender']) : 1,
//            'create_time'=>date('Y-m-d H:i:s',time()),
            'update_time'=>date('Y-m-d H:i:s',time()),
//            'start_date'=>mb_substr($this->params['times'],0,19),
//            'end_date'=>mb_substr($this->params['times'],22),
            'remark'=>isset($params['remark']) ? $params['remark'] : '',
        ];
        if(!$this->params['times']){
            $data['start_date']= mb_substr($this->params['times'],0,19);
            $data['end_date']=mb_substr($this->params['times'],22);
        }
        $upkid=$oMobile->db_update('',$params['kid'],$data);
        if($upkid){
            $this->returnJson(0,'修改成功',URL('admin/mobile/list'));
            }

        $this->returnJson(1,'修改失败');
    }



    //判断 添加修改的 参数是否完整   type 0(添加) 1(修改)   params 数据集合
    public function checkParams($type=0,$params){
        $oMobile = new Mobile();
        if(!is_array($params)){
            $this->returnJson(1,'数据错误');
        }
        if($type && !isset($params['kid'])){
            $this->returnJson(1,'数据错误');
        }
        if(!$type){
            $checkUname=\App\Components\Tools::checkUname($params['user_name']);
            !$checkUname ? $this->returnJson(1,'请正确输入5-11位 字母、数字、下划线 的用户名') : '';
            $check_uname=$oMobile->checkUname($params['user_name']);
            $check_uname ? $this->returnJson(1,'该用户名已存在') : '';
        }

        $checkUpwd=\App\Components\Tools::checkUpwd($params['pwd']);
        if( (!$type && !$checkUpwd) || ($type && $params['pwd'] && !$checkUpwd) ){
            $this->returnJson(1,'请正确输入6-16位字母、数字、下划线的 密码');
        }

        $checkUmobile=\App\Components\Tools::checkUmobile($params['mobile']);
        if(!$checkUmobile){
            $this->returnJson(1,'请正确输入手机号');
        }

    }

    //拉取会员套餐
    function listData(){
        $partner_id=isset($this->params['kid']) ? intval($this->params['kid']) : 0;
        $oPartner = new Partner();
        $data=$oPartner->selectMeal($partner_id);
        $this->returnJson(0,$data);
    }

    /*判断修改的套餐是否正确
    *	first 初级        	middle 中级			tall 高级
    *	oldTaocanName 修改前的会员套餐信息		true 升级套餐，false 套餐不变
    */
    function judgeTaocan($oldTaocanName,$newTaocanName){
        if( $oldTaocanName=='first' && $newTaocanName!="first" ){
            return true;
        }elseif($oldTaocanName=='first' && $newTaocanName=="first"){
            return false;
        }elseif( $oldTaocanName=='middle' && $newTaocanName=="tall" ){
            return true;
        }elseif( $oldTaocanName=='middle' && $newTaocanName=="middle" ){
            return false;
        }elseif( $oldTaocanName=='tall' && $newTaocanName=="tall" ){
            return false;
        }else{
            $this->returnJson(1,"套餐不能降级");
        }
    }








}